package com.suapi.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suapi.project.common.ErrorCode;
import com.suapi.project.exception.BusinessException;
import com.suapi.project.mapper.InterfaceInfoMapper;
import com.suapi.project.mapper.UserInterfaceInfoMapper;
import com.suapi.project.model.enums.UserInterfaceInfoStatusEnum;
import com.suapi.project.service.InterfaceInfoService;
import com.suapi.suapicommon.model.entity.InterfaceInfo;
import com.suapi.suapicommon.model.entity.UserInterfaceInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author suapi
* @description 针对表【interface_info(接口信息)】的数据库操作Service实现
* @createDate 2025-04-07 15:14:15
*/
@Slf4j
@Service
public class InterfaceInfoServiceImpl extends ServiceImpl<InterfaceInfoMapper, InterfaceInfo>
    implements InterfaceInfoService{

    @Resource
    private UserInterfaceInfoMapper userInterfaceInfoMapper;

    /**
     * 参数校验
     * @param interfaceInfo
     * @param add
     */
    @Override
    public void validInterfaceInfo(InterfaceInfo interfaceInfo, boolean add) {
        if (interfaceInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String name = interfaceInfo.getName();
        String description = interfaceInfo.getDescription();
        String url = interfaceInfo.getUrl();
        String requestHeader = interfaceInfo.getRequestHeader();
        String responseHeader = interfaceInfo.getResponseHeader();
        String method = interfaceInfo.getMethod();
        // 创建时，所有参数必须非空
        log.info(String.valueOf(interfaceInfo));
        if (add) {
            if (StringUtils.isAnyBlank(name,description,url,requestHeader,responseHeader,method)) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
        }
        if (StringUtils.isNotBlank(name) && name.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "接口名称过长");
        }
        //TODO 要其他的规则后面再加
    }

    /**
     * 开通接口权限
     * @param interfaceInfoId
     * @param userId
     * @return
     */
    @Override
    public Boolean openPermission(Long interfaceInfoId, Long userId) {
        if (interfaceInfoId < 0 || userId < 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //在数据库中查询是否已经有了开通记录
        LambdaQueryWrapper<UserInterfaceInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInterfaceInfo::getUserId,userId);
        wrapper.eq(UserInterfaceInfo::getInterfaceInfoId,interfaceInfoId);
        UserInterfaceInfo userInterfaceInfo;
        userInterfaceInfo = userInterfaceInfoMapper.selectOne(wrapper);
        // TODO 我们这里由于没有开通付费服务，默认第一次开通之后给予100次调用次数，用完之后不能再开通，所以这里先不做处理
        if (userInterfaceInfo != null){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"请勿重复开通");
        }
        userInterfaceInfo = new UserInterfaceInfo();
        userInterfaceInfo.setInterfaceInfoId(interfaceInfoId);
        userInterfaceInfo.setUserId(userId);
        userInterfaceInfo.setLeftNum(100);
        //数据库已经有默认值
        //userInterfaceInfo.setStatus(UserInterfaceInfoStatusEnum.on.getValue());
        //userInterfaceInfo.setTotalNum(0);
        if (userInterfaceInfoMapper.insert(userInterfaceInfo) <= 0){
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return true;
    }


}




